/*
 * 文件名：CompanyController.java
 * 版权：Copyright by www.nd.com
 * 描述：
 * 修改人：Administrator
 * 修改时间：2016年12月9日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package com.liupf.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.liupf.common.utils.Constants;
import com.liupf.common.utils.SmsUtil;
import com.liupf.common.utils.TimeUtil;
import com.liupf.domain.Tel;
import com.liupf.domain.TelCompanyDedut;
import com.liupf.domain.TelCompanyRecharge;

public class CompanyController extends Controller
{
    private Log log = LogFactory.getLog(this.getClass());
    
    public void index(){
      //操作员自己所属品牌
        String company=(String) getSession().getAttribute("company");
        String syscity=(String) getSession().getAttribute("syscity");
        String userRole = (String)getSession().getAttribute("userRole");
        
        StringBuffer sb=new StringBuffer();
        sb.append("from tel_company where 1=1");
        
        if(!"admin".equals(userRole)){
            sb.append(" and company="+company);
        }
        
        Page<Tel> page=Tel.dao.paginate(getParaToInt(0, 1), 10,
            "select  * ", sb.toString());
        
        setAttr("page", page);
        render("sys_tel_company_recharge.ftl");
    }
    
    public void toAddMessage(){
        
        render("sys_tel_company_add.ftl");
    }
    
    public void noMessageCompany(){
        String sql = "";
        sql = "SELECT company,companyname FROM tel_company where recharge_message is null";

        List<Record> list = Db.find(sql);
        setAttr("record",list);
        renderJson();
    }
    
    public void addMessage(){
        String company=getPara("company");
        String message=getPara("message");
        String username=(String) this.getRequest().getSession().getAttribute("userName");
        message=message.replaceAll("\r\n", "<br>");
        
        String sql="update tel_company set recharge_message=?,recharge_message_user=? where company='"+company+"'";
        Db.update(sql,message,username);
        
        redirect("/company");
    }
    
    public void toEditMessage(){
        int id=getParaToInt();
        Tel tel=Tel.dao.findById(id);
        
        setAttr("tel", tel);
        render("sys_tel_company_edit.ftl");
    }
    
    public void editMessage(){
        Tel tel=getModel(Tel.class);
        String username=(String) this.getRequest().getSession().getAttribute("userName");
        tel.set("recharge_message_user", username);
        tel.update();
        
        redirect("/company");
    }
    
    public void toAddRecharge(){
        int id=getParaToInt();
        
        Tel tel=Tel.dao.findById(id);
        
        setAttr("tel", tel);
        render("tel_recharge_add.ftl");
    }
    
    public void addCharge(){
        TelCompanyRecharge recharge=getModel(TelCompanyRecharge.class);
        String username=(String) this.getRequest().getSession().getAttribute("userName");
        String id=getPara("id");
        Tel tel=Tel.dao.findById(id);
        float balance=tel.getFloat("recharge_money");
        String bank=getPara("bank");
        String serialno=getPara("serialno");
        float money=Float.parseFloat(getPara("money"));
        String remark=getPara("remark");
        
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(new Date());
        recharge.put("createtime", dateString);
        recharge.put("companyname", tel.getStr("companyname"));
        recharge.put("companyid", id);
        recharge.put("bank", bank);
        recharge.put("serialno", serialno);
        recharge.put("money", money);
        recharge.put("username", username);
        recharge.put("before_balance", balance);
        recharge.put("remark", remark);
        recharge.save();
        
        balance+=money;
        tel.set("recharge_money", balance);
        tel.update();
        
        redirect("/company");
    }
    
    public void history(){
    	int id=getParaToInt("id");
    	StringBuffer sql=new StringBuffer("from tel_company_recharge where 1=1 and companyid="+id);
    	
    	String startTime=getPara("startTime");
    	String endTime=getPara("endTime");
    	
    	if(!"".equals(startTime) && !"".equals(endTime) && startTime!=null && endTime!=null){
    		Date beginDate = TimeUtil.parse(startTime, "yyyy-MM-dd", new Date());
    		Date endDate = TimeUtil.parse(endTime, "yyyy-MM-dd", new Date());
    		beginDate = setTime(beginDate, 0, 0, 0, 0);
    		endDate = setTime(endDate, 23, 59, 59, 999);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			sql.append(" and createtime >='" + sdf.format(beginDate) + "'");
    		
			sql.append(" and createtime <'" + sdf.format(endDate) + "'");
    			
    	}
    	
    	Page<TelCompanyRecharge> page=TelCompanyRecharge.dao.paginate(getParaToInt(0, 1), 10,
            "select  * ", sql.toString());
    	
    	setAttr("page",page);
    	setAttr("startTime", startTime);
    	setAttr("endTime", endTime);
    	setAttr("id", id);
    	
    	render("sys_tel_company_history.ftl");
    }
    
    private Date setTime(Date date, int hour, int minute, int second, int ms) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(date);
		calendar.set(Calendar.MINUTE, minute);
		calendar.set(Calendar.SECOND, second);
		calendar.set(Calendar.HOUR_OF_DAY, hour);
		calendar.set(Calendar.MILLISECOND, ms);
		date = calendar.getTime();
		return date;
	}
    
    
    public void balance(){
        String company=(String) getSession().getAttribute("company");
        String sql="select recharge_money from tel_company where company='"+company+"'";
        
        float balance=Db.queryFloat(sql);
        
        setAttr("balance",balance);
        renderJson();
    }
    
    public void querySms(){
    	String smsNum=SmsUtil.getSmsBalanceToGateway();
    	  setAttr("smsNum",smsNum);
          renderJson();
    }
    
    public void toDedut(){
    	int id=getParaToInt();
        
        Tel tel=Tel.dao.findById(id);
        
        setAttr("tel", tel);
        render("tel_dedut_add.ftl");
    }
    
    public void addDedut(){
    	TelCompanyDedut dedut =new TelCompanyDedut();
    	String id=getPara("companyid");
    	dedut.set("companyid", getPara("companyid"));
    	 Tel tel=Tel.dao.findById(id);
         float balance=tel.getFloat("recharge_money");
         dedut.set("companyname", tel.getStr("companyname"));
         float money=Float.parseFloat(getPara("dedut"));
         dedut.set("dedut", money);
         String remark=getPara("remark");
         
         SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         String dateString = formatter.format(new Date());
         dedut.put("time", dateString);
         String username=(String) this.getRequest().getSession().getAttribute("userName");
         dedut.put("user", username);
         dedut.put("before", balance);
         dedut.put("remark", remark);
         dedut.save();
         
         balance-=money;
         tel.set("recharge_money", balance);
         tel.update();
         
         redirect("/company");
    }
    
    public void dedutHistory(){
    	int id=getParaToInt("id");
    	StringBuffer sql=new StringBuffer("from tel_company_dedut where 1=1 and companyid="+id);
    	
    	String startTime=getPara("startTime");
    	String endTime=getPara("endTime");
    	
    	if(!"".equals(startTime) && !"".equals(endTime) && startTime!=null && endTime!=null){
    		Date beginDate = TimeUtil.parse(startTime, "yyyy-MM-dd", new Date());
    		Date endDate = TimeUtil.parse(endTime, "yyyy-MM-dd", new Date());
    		beginDate = setTime(beginDate, 0, 0, 0, 0);
    		endDate = setTime(endDate, 23, 59, 59, 999);
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			sql.append(" and time >='" + sdf.format(beginDate) + "'");
    		
			sql.append(" and time <'" + sdf.format(endDate) + "'");
    			
    	}
    	
    	Page<TelCompanyDedut> page=TelCompanyDedut.dao.paginate(getParaToInt(0, 1), 10,
            "select  * ", sql.toString());
    	
    	setAttr("page",page);
    	setAttr("startTime", startTime);
    	setAttr("endTime", endTime);
    	setAttr("id", id);
    	
    	render("sys_tel_company_dedut_history.ftl");
    }
}
